package com.hhsjtest.bus_enter.businessdata

import com.hhsjdesign.dynamicui.annoprocess.ABusinessBean
import com.hhsjdesign.dynamicui.annoprocess.AFromJSON
import com.hhsjdesign.dynamicui.businessdata.IViewable
import org.json.JSONObject


/**
 * 功能入口
 */
@ABusinessBean("enter")
class Enter : IViewable.IViewableTransformer, IViewable.IRedDotTree {
    //唯一id
    var itemId: String? = null

    //所属组件id/所属父级id
    var parentId: String? = null

    var treeId: String? = null

    //消息编码
    var msgCode: String? = null

    //标题
    var title: String? = null

    //跳转
    var url: String? = null

    //描述
    var desc: String? = null

    //图标
    var icon: String? = null

    //背景
    var bgUrl: String? = null

    //子元素
    var childList: MutableList<Enter>? = null

    override fun toViewable(): IViewable<Enter>? {
        return IViewable<Enter>().apply {
            this.businessData = this@Enter
            this.viewId = this@Enter.itemId
            this.widgetId = this@Enter.parentId
            this.treeId = this@Enter.treeId
            this.title = this@Enter.title
            this.description = this@Enter.desc
            this.imageUrl = this@Enter.bgUrl
            this.funIcon = this@Enter.icon
        }
    }

    override fun treeId(parentId: String?, treeId: String?) {
        this.parentId = parentId
        this.treeId = treeId
    }

    companion object {

        @JvmStatic
        @AFromJSON
        fun fromJSON(parentId: String?, treeId: String?, jo: JSONObject?): Enter? {
            jo ?: return null
            return Enter().apply {
                this.parentId = parentId
                this.treeId = treeId
                this.itemId = jo.optString("id")
                this.url = jo.optString("url")
                this.bgUrl = jo.optString("bgUrl")
                this.icon = jo.optString("icon")
                this.title = jo.optString("title")
                this.desc = jo.optString("desc")
                this.msgCode = jo.optString("msgCode")
                var array = jo.optJSONArray("child")
                if (array != null && array.length() > 0) {
                    this.childList = ArrayList()
                    for (i in 0 until array.length()) {
                        fromJSON(itemId, "${treeId}_${itemId}", array.optJSONObject(i))?.let {
                            this.childList!!.add(it)
                        }
                    }
                }
            }
        }
    }
}